﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Interaction logic for App.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Desktop
{
    using System;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Threading;
    using Hisaab.Business.Mapping;
    using Hisaab.Client.Common.Translation;
    using Hisaab.Client.Mapping;
    using Hisaab.Common;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Application.Startup"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs"/> that contains the event data.</param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            try
            {
                TranslationManager.Instance.TranslationProvider =
                    new ResxTranslationProvider(
                        "Hisaab.Client.Desktop.Properties.Resources", Assembly.GetExecutingAssembly());

                ObjectMapper.AddProfile(new ClientBusinessMapping());
                ObjectMapper.AddProfile(new BusinessMapping());

#if SECURE

                var login = new Login();
                bool? ret = login.ShowDialog();

                if (!(ret.HasValue && ret.Value && System.Threading.Thread.CurrentPrincipal != null))
                {
                    return;
                }
#endif
                var bootstrapper = new Bootstrapper();
                bootstrapper.Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Hisaab", MessageBoxButton.OK, MessageBoxImage.Error);
                Shutdown();
            }
        }

        /// <summary>
        /// Called when [on application unhandled exception].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void OnOnApplicationUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, "Hisaab", MessageBoxButton.OK, MessageBoxImage.Error);
            Shutdown();
        }
    }
}